import { wxRequest } from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({

  data: {
    imgUrl: app.globalData.imgUrl,
    pageNo: 1,
    hasNextPage:false,
    list: []
  },

  onLoad:function(option){
    if(option.type){
      this.setData({
        flageTpe: option.type
      })
    }
  },

  //选择地址
  choose:function(e){
    if (!this.data.flageTpe){
      return
    }
    var item = e.currentTarget.dataset.item;
    console.log(item,'item')
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面    
    prevPage.setData({
      defaultAddress: item,
    })
    wx.navigateBack(); 
  },

  onShow() {
    this.getlist();
  },
  //添加
  toAdd: function() {
    wx.navigateTo({
      url: '/pages/address/add?type='+this.data.flageTpe,
    })
  },

  //编辑
  handleEdit: function (e) {
    var item = e.currentTarget.dataset.item
    wx.setStorageSync('addressItem', item)
    wx.navigateTo({
      url: '/pages/address/add?type=' + this.data.flageTpe,
    })
  },

  //获取数据
  getlist: function() {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/customerAddress/list',
      method: 'POST',
      data:{}
    }, (res) => {
      let list = res.list;
      if (this.data.pageNo !== 1) {
        list = this.data.list.concat(list);
      }

      this.setData({
        list: list,
        // pageNo: res.page.pageNo,
        // hasNextPage: res.page.hasNextPage,
      });
    })
  },

  //下拉刷新
  onPullDownRefresh: function() {
    this.refreshData();
    wx.stopPullDownRefresh()
  },

  refreshData: function() {
    this.setData({
      pageNo: 1
    })
    this.getlist();
  },
  //触底时，加载更多
  onReachBottom: function () {
    if (this.data.hasNextPage) {
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      this.getlist();
    }
  },

  //删除
  handleDel: function (e) {
    var item = e.currentTarget.dataset.item
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/comm/beanCrud/mall.CustomerAddressVO/delete',
      method: 'POST',
      contentType: 'application/x-www-form-urlencoded',
      data: {
        id: item.id
      }
    }, (res) => {
      console.log(res, 'delete')
      this.getlist()
    })
  },



})